/**
 * 用于处理服务器的各种事件，包括服务器状态、服务器回调的命令等
 * */
package riki.org.com.handle
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.dataGridClasses.DataGridColumn;
	
	import riki.org.com.command.NcSend;
	import riki.org.com.date.OnlineApps;
	import riki.org.com.model.ModelLocator;
	import riki.org.com.model.Page;
	
	public class NetHandle
	{
		public var model:ModelLocator	= ModelLocator.getInstance();
		
		public function NetHandle()
		{
			//model = ModelLocator.getInstance()
		}
		
		/**
		 * [Event Handle]
		 * 
		 * NetConnection 连接出错（IO 错误）
		 * */
		public function onFindErrorEvent(e:*):void
		{
			//Alert.show("0001:\n服务器连接失败!")
		}
		
		/**
		 * [Event Handle]
		 * 
		 * 连接成功后的处理
		 * */
		public function ConnectSuccess(Message:String):void
		{
			//Alert.show(Message,"Message:");
			//连接成功后显示主页面
			model.vObj.MainApp.currentState	=	Page.MAIN_PAGE;
			//发送权限提升命令
			NcSend.loginWithAdmin("loginReturn");
		}
		
		/**
		 * 权限提升结果
		 * */
		public function loginReturn(...argc):void
		{
			trace("loginReturn->argc:"+argc);
			model.isOnline	=	argc[1];
			//游戏列表argc[2]
			var gamelist:Array	=	argc[2];						
			var	i:int = 0 ;			
					
			for(i = 0 ; i < gamelist.length ; i ++)
			{
				model.sDataBase.ServerApplicationList.push(new	OnlineApps(gamelist[i].appName,gamelist[i].name));
			}
			/**
			//下载表			
			var downloadinfolist:Array	=	argc[3]	as	Array;
			//游戏表
			var playinfolist	:Array	=	argc[4]	as	Array;			
			//文件表
			var fileinfolist	:Array	=	argc[5]	as	Array;
			
			var	p:String	=	"";
			var s:String	=	"";
			
			trace("下载信息=========================================================")
			var	a:DateFormatter	=	new	DateFormatter();
			a.formatString	=	"MM-DD-YYYY";
			
			model.sDataBase.downloadinfolist	=	new	ArrayCollection();
			for(i = 0 ; i < downloadinfolist.length ; i ++)
			{
				var	tempinfo1:Downloadinfo	=	new	Downloadinfo();
				
				tempinfo1.Id	=	downloadinfolist[i].Id;
				tempinfo1.appid	=	downloadinfolist[i].appid;
				tempinfo1.dtime	=	a.format(downloadinfolist[i].dtime);
				
				model.sDataBase.downloadinfolist.addItem(tempinfo1);
				
				trace(i+" : "+"Id:"+"["+tempinfo1.Id+"]"+"  appid:"+"["+tempinfo1.appid+"]"+"   dtime:"+"["+tempinfo1.dtime+"]");
			}
			
			trace("游戏信息=========================================================")
			for(i = 0 ; i < playinfolist.length ; i ++)
			{
				var	tempinfo2:Playinfo	=	new	Playinfo();
				
				tempinfo2.Id		=	playinfolist[i].Id;
				tempinfo2.appid		=	playinfolist[i].appid;
				tempinfo2.usedtime	=	a.format(playinfolist[i].usedtime);
				
				model.sDataBase.downloadinfolist.addItem(tempinfo1);
				
				trace(i+" : "+"Id:"+"["+tempinfo2.Id+"]"+"  appid:"+"["+tempinfo2.appid+"]"+"   usedtime:"+"["+tempinfo2.usedtime+"]");
			}
			
			trace("文件信息=========================================================")
			for(i = 0 ; i < playinfolist.length ; i ++)
			{
				var	tempinfo3:FileInFo	=	new	FileInFo();
				
				tempinfo2.Id		=	playinfolist[i].Id;
				tempinfo2.appid		=	playinfolist[i].appid;
				tempinfo2.usedtime	=	a.format(playinfolist[i].usedtime);
				
				model.sDataBase.downloadinfolist.addItem(tempinfo1);
				
				trace(i+" : "+"Id:"+"["+tempinfo2.Id+"]"+"  appid:"+"["+tempinfo2.appid+"]"+"   usedtime:"+"["+tempinfo2.usedtime+"]");
			}
			
			trace("=========================================================")
			 * */
			
			model.sDataBase.toBindingList();
		}
		
		/**
		 * 返回游戏在线人数
		 * */
		public function ReturnOnlines(...argc):void
		{
			
		}
		
		/**
		 * 服务器日志接收
		 * */
		public function OnServerLog(...argc):void
		{
			trace("OnServerLog->argc:"+argc);
			try
			{
				var	tempapp:OnlineApps	=	model.sDataBase.getApplicationInfo(argc[2]);
				tempapp.gameLog.unshift(argc[3].toString())
				tempapp.getLogListDate	();
			}
			catch(e:Error)
			{
				trace("OnServerLog Error:=============")
				trace(e.message,"\n",e.getStackTrace())				
				trace("=================================")
			}
		}
		
		
		
		/**
		 * 返回游戏统计记录
		 * */
		public function ReTurnInfoCount(...argc):void
		{
			trace("返回游戏统计记录:"+argc)
			model.vObj.MainApp.downloads1.label	=	argc[2];
			model.vObj.MainApp.downloads2.label	=	argc[3];
			model.vObj.MainApp.used1.label		=	argc[4];
			model.vObj.MainApp.used2.label		=	argc[5];			
		}		
		
		/**
		 * 返回应用程序数据
		 * */
		public function returnAppInfo(...argc):void
		{
			trace("returnAppInfo->argc:"+argc);
			try
			{
				var	tempapp:OnlineApps	=	model.sDataBase.getApplicationInfo(argc[0]);
				
				if(tempapp!=null)
				{
					tempapp.PlayeList	=	new	ArrayCollection();
					tempapp.RoomList	=	new	ArrayCollection();
				}
				var	i:int				=	0;
				var	p:String		=	"";
				var tObj:Object;
				
				if(argc[1]!=null)
				{
					for(i = 0 ; i < argc[1].length ; i ++)
					{
						trace("PlayeList "+i+":"+argc[1][i]);
						tempapp.PlayeList.addItem(argc[1][i])
					}
				}
				
				if(argc[2]!=null)
				{
					for(i = 0 ; i < argc[2].length ; i ++)
					{
						trace("RoomList "+i+":"+argc[2][i]);
						tempapp.RoomList.addItem(argc[2][i])
					}
				}
				
				model.vObj.MainApp.OnlinesPlayer.label			=	tempapp.PlayerCount.toString();
				model.vObj.MainApp.OnlinesRoom.label			=	tempapp.RoomCount.toString();				
				
				var tempcolumn:DataGridColumn;
				
				if(!(tempapp.PlayeList.length<1))
				{
					tObj	=	tempapp.PlayeList.getItemAt(0);
				}
				model.vObj.MainApp.GameUserList.columns			=	new	Array();
				for(p	in	tObj)
				{
					//trace(p)
					tempcolumn				= new DataGridColumn();
					tempcolumn.headerText	=	p;
					tempcolumn.dataField	=	p;
					model.vObj.MainApp.GameUserList.columns	= model.vObj.MainApp.GameUserList.columns.concat(tempcolumn);
				}
				
				if(!(tempapp.RoomList.length<1))
				{
					tObj	=	tempapp.RoomList.getItemAt(0);
				}				
				model.vObj.MainApp.GameRoomList.columns			=	new	Array();
				for(p	in	tObj)
				{
					//trace(p)
					tempcolumn				= new DataGridColumn();
					tempcolumn.headerText	=	p;
					tempcolumn.dataField	=	p;
					model.vObj.MainApp.GameRoomList.columns	= model.vObj.MainApp.GameRoomList.columns.concat(tempcolumn);
				}
				
				model.vObj.MainApp.GameUserList.horizontalScrollPolicy	=	"on"
				model.vObj.MainApp.GameRoomList.horizontalScrollPolicy	=	"on"
				
				model.vObj.MainApp.GameUserList	.dataProvider	=	tempapp.PlayeList;
				model.vObj.MainApp.GameRoomList	.dataProvider	=	tempapp.RoomList;
				model.vObj.MainApp.LogList		.dataProvider	=	tempapp.LogdataProvider;
			}
			catch(e:Error)
			{
				trace("ReturnAppInfo Error:=============")
				trace(e.message,"\n",e.getStackTrace())				
				trace("=================================")
			}
		}
		public function onNewMessage(...argc):void
		{
		}
		
		/**
		 * 服务器出错
		 * */
		public function onServerError(...argc):void
		{
			trace("onServerError->argc:"+argc);
		}
		
		/**
		 * [Event Handle]
		 * 
		 * 连接关闭后的处理
		 * */
		public function ConnectClosed(Message:String):void
		{
			Alert.show(Message,"Message:");
		}
		
		/**
		 * [Event Handle]
		 * 
		 * 连接关闭后的处理
		 * */
		public function ConnectFailed(CodeNumber:String,Message:String):void
		{
			Alert.show(CodeNumber+":\n"+Message,"Message:");
		}
	}
}